package com.youkeyi.ddy.cloud.application.business.hr.employee.position.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.position.BusinessEmployeePositionHistoryCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.position.dto.BusinessEmployeePositionHistoryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.position.dto.BusinessEmployeePositionHistoryListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.position.param.BusinessEmployeePositionHistoryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.position.param.BusinessEmployeePositionHistoryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.position.param.BusinessEmployeePositionHistoryListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.position.param.BusinessEmployeePositionHistoryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.position.HrEmployeePositionHistoryCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.position.dto.HrEmployeePositionHistoryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.position.param.HrEmployeePositionHistoryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.position.param.HrEmployeePositionHistoryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.position.param.HrEmployeePositionHistoryQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.position.param.HrEmployeePositionHistoryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * BusinessEmployeePositionHistoryController
 *
 * @Author liuxiawang
 * @Date 2024/4/29 6:14 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeePositionHistoryController implements BusinessEmployeePositionHistoryCloudService {

    @Autowired
    private HrEmployeePositionHistoryCloudService hrEmployeePositionHistoryCloudService;

    @Override
    public BusinessEmployeePositionHistoryListCloudDto list(BusinessEmployeePositionHistoryListCloudParam param) {
        BusinessEmployeePositionHistoryListCloudDto dto = new BusinessEmployeePositionHistoryListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeePositionHistoryQueryCloudParam employeeQueryCloudParam = new HrEmployeePositionHistoryQueryCloudParam();
        employeeQueryCloudParam.setEmployeePositionHistoryPrimaryKeys(param.getEmployeePositionHistoryPrimaryKeys());
        employeeQueryCloudParam.setEmployeePrimaryKeys(param.getEmployeePrimaryKeys());
        employeeQueryCloudParam.setPageSize(param.getPageSize());
        employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        HrEmployeePositionHistoryListCloudDto employeeDtos = hrEmployeePositionHistoryCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            List<BusinessEmployeePositionHistoryListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employee -> {
                BusinessEmployeePositionHistoryListItemCloudDto item = new BusinessEmployeePositionHistoryListItemCloudDto();
                item.setEmployeePositionHistoryPrimaryKey(employee.getEmployeePositionHistoryPrimaryKey());
                item.setEmployeeKey(employee.getEmployeeKey());
                item.setStartDate(employee.getStartDate());
                item.setEndDate(employee.getEndDate());
                item.setDepartment(employee.getDepartment());
                item.setPosition(employee.getPosition());
                item.setPositionType(employee.getPositionType());
                item.setEmploymentStatus(employee.getEmploymentStatus());
                item.setRemark(employee.getRemark());
                item.setIsDeleted(employee.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(employee.getCreateTime());
                item.setCreateUserKey(employee.getCreateUserKey());
                item.setCreateUserName(employee.getCreateUserName());
                item.setUpdateTime(employee.getUpdateTime());
                item.setUpdateUserKey(employee.getUpdateUserKey());
                item.setUpdateUserName(employee.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(employeeDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessEmployeePositionHistoryCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeeKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeePositionHistoryCreateCloudParam employeeCreateCloudParam = new HrEmployeePositionHistoryCreateCloudParam();
        employeeCreateCloudParam.setEmployeeKey(param.getEmployeeKey());
        employeeCreateCloudParam.setStartDate(param.getStartDate());
        employeeCreateCloudParam.setEndDate(param.getEndDate());
        employeeCreateCloudParam.setDepartment(param.getDepartment());
        employeeCreateCloudParam.setPosition(param.getPosition());
        employeeCreateCloudParam.setPositionType(param.getPositionType());
        employeeCreateCloudParam.setEmploymentStatus(param.getEmploymentStatus());
        employeeCreateCloudParam.setRemark(param.getRemark());
        employeeCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeePositionHistoryCloudService.create(employeeCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessEmployeePositionHistoryUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getEmployeePositionHistoryPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeePositionHistoryUpdateCloudParam employeeUpdateCloudParam = new HrEmployeePositionHistoryUpdateCloudParam();
        employeeUpdateCloudParam.setEmployeePositionHistoryPrimaryKey(param.getEmployeePositionHistoryPrimaryKey());
        employeeUpdateCloudParam.setStartDate(param.getStartDate());
        employeeUpdateCloudParam.setEndDate(param.getEndDate());
        employeeUpdateCloudParam.setDepartment(param.getDepartment());
        employeeUpdateCloudParam.setPosition(param.getPosition());
        employeeUpdateCloudParam.setPositionType(param.getPositionType());
        employeeUpdateCloudParam.setEmploymentStatus(param.getEmploymentStatus());
        employeeUpdateCloudParam.setRemark(param.getRemark());
        employeeUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeePositionHistoryCloudService.update(employeeUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessEmployeePositionHistoryDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getEmployeePositionHistoryPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        HrEmployeePositionHistoryDeleteCloudParam employeeDeleteCloudParam = new HrEmployeePositionHistoryDeleteCloudParam();
        employeeDeleteCloudParam.setEmployeePositionHistoryPrimaryKeys(param.getEmployeePositionHistoryPrimaryKeys());
        employeeDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        employeeDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return hrEmployeePositionHistoryCloudService.delete(employeeDeleteCloudParam);
    }
}
